<?php
namespace controllers;

require('./views/View.php');
require('./models/UsersModel.php');

/**
 * 用户控制器
 */
class UsersController
{
    protected $username;

    protected $password;

    protected $data = [];

    // protected $config = [];

    // public function __construct($config)
    // {
    //     $this->config = $config;
    // }å

    /**
     * 渲染登录页面
     */
    // public function toLogin()
    // {
    //     $loginView = 'users/toLogin.php';
    //     $view = new \views\View($loginView);
    //     $view->render();
    // }

    /**
     * 用户界面
     */
    public function index()
    {
        $nextView = 'users/index.php';
        $title = '用户主页';
        $model = new \models\UsersModel();
        $retval = $model->getIndex();
        $view = new \views\View($title, $nextView);
        $view->render($retval);
    }

    /**
     * 登录
     */
    public function login(){
        $title = '登录页面';
        // 判断是否提交登录表单
        if(isset($_POST['username'])&&isset($_POST['password'])) {
            // 如果提交了登录表单，进行登录
            $data['username'] = $_POST['username'];
            $data['password'] = $_POST['password'];
            // 登录验证
            $model = new \models\UsersModel();
            $retval = $model->tryLogin($data);
            // 判断是否登录成功
            if ($retval['status'] == true) {
                //保存用户名
                $_SESSION['user'] = $_POST['username'];
                // 判断是否需要定义常量 USER
                if(!defined('USER')) {
                    define('USER', $_SESSION['user']);
                }
                // 跳转至登录页面
                // $nextView = 'users/toLogin.php';
                // $view = new \views\View($title, $nextView);
                // $view->render($retval);
                // 跳转至任务主页
                $taskController = new \controllers\TaskController();
                $taskController->index();
            } else {
                // 如果没登录成功，重新登录并返回错误信息
                $nextView = 'users/toLogin.php';
                $view = new \views\View($title, $nextView);
                $view->render($retval);
            }
        } else {
            // 如果没有提交表单，判断是否含有登录信息
            if(isset($_SESSION['user'])) {
                // 如果含有登录信息，跳转至任务主页
                $taskController = new \controllers\TaskController();
                $taskController->index();
            } else {
                // 如果没有登录信息，进行表单填写登录
                $loginView = 'users/toLogin.php';
                $view = new \views\View($title, $loginView);
                $view->render();
            }
        }
    }

    /**
     * 注销
     */
    public function logout()
    {
        $_SESSION['user'] = null;
        $this->login();
    }

    /**
     * 注册
     */
    public function register(){
        $loginTitle = '登录';
        $registerTitle = '注册';
        $nextView = 'users/register.php';

        if(isset($_POST['username']) && isset($_POST['password'])) {
            if($_POST['username'] == '') {
                $view = new \views\View($registerTitle, $nextView);
                $retval = array('message' => '请输入用户名!', 'status' => false);
                return $view->render($retval);
            }
            if($_POST['password'] == '') {
                $view = new \views\View($registerTitle, $nextView);
                $retval = array('message' => '请输入密码!', 'status' => false);
                return $view->render($retval);
            }
            $data['username'] = $_POST['username'];
            $data['password'] = $_POST['password'];
            $model = new \models\UsersModel();
            // 开始注册
            $retval = $model->addUser($data);
            // 检查是否注册成功
            // if($retval['status'] == true){
            //     // 注册成功，跳转登录
            //     $nextView = 'users/toLogin.php';
            //     $view = new \views\View($loginTitle, $nextView);
            //     // 渲染视图
            //     $view->render();
            // } else {
            //     // 注册失败，返回消息
            //     $view = new \views\View($registerTitle, $nextView);
            //     // 渲染视图
            //     $view->render($retval);
            // }
            $view = new \views\View($registerTitle, $nextView);
            $view->render($retval);
        } else {
            $view = new \views\View($registerTitle, $nextView);
            $view->render();
        }
    }
}